/*
 * ClientWS.java
 * 
 * Created: Feb 16, 2015
 * 
 * Copyright (c) ExpertPB 2014
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.web;

import com.emex.app.shared.data.Client;
import com.emex.app.shared.data.JuridicalPerson;
import com.emex.app.shared.data.OrderPosition;
import com.emex.app.shared.data.Orders;
import com.emex.app.shared.ws.ClientWebService;
import com.emex.app.web.service.ClientService;
import com.emex.app.web.service.JuridicalPersonService;
import com.emex.app.web.service.OrderPositionService;
import com.emex.app.web.service.OrderService;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

/**
 *
 * @author Sergey Karavaev
 */
@WebService(serviceName = "ClientWS", portName = "ClientWSPort", targetNamespace = "http://emex.com/Client")
@Stateless(name = "ClientWS")
public class ClientWS extends SpringBeanAutowiringSupport implements ClientWebService {

    @Autowired
    private ClientService clientService;

    @Autowired
    private JuridicalPersonService juridicalPersonService;

    @Autowired
    private OrderPositionService orderPositionService;

    @Autowired
    private OrderService orderService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return clientService.deleteByPrimaryKey(id);
    }

    @Override
    public Client insert(Client record) {
        return clientService.insert(record);
    }

    @Override
    public List<Client> selectByPhoneOrSurname(String criteria) {
        return clientService.selectByPhoneOrSurname(criteria);
    }

    @Override
    public Client selectByPrimaryKey(Integer id) {
        return clientService.selectByPrimaryKey(id);
    }

    @Override
    public Client updateByPrimaryKey(Client record) {
        return clientService.updateByPrimaryKey(record);
    }

    @Override
    public List<Client> getAllClients() {
        return clientService.getAllClients();
    }

    @Override
    public List<Client> getAllClientsByOrderPositionName(String name) {
        List<OrderPosition> orderPostions = orderPositionService.getOrderPositionsByName(name);
        List<Client> clients = new ArrayList<>();
        for (OrderPosition op : orderPostions) {
            // 1. select order to get client id
            Orders order = orderService.selectOrderByPrimaryKey(op.getOrderId());
            if (null != order) {
                Client client = clientService.selectByPrimaryKey(order.getClientId());
                if (null != client) {
                    clients.add(client);
                }
            }
        }
        return clients;
    }

    public List<JuridicalPerson> getAllJuridicalClientsByOrderPositionName(String name) {
        List<OrderPosition> orderPostions = orderPositionService.getOrderPositionsByName(name);
        List<JuridicalPerson> clients = new ArrayList<>();
        for (OrderPosition op : orderPostions) {
            // 1. select order to get client id
            Orders order = orderService.selectOrderByPrimaryKey(op.getOrderId());
            if (null != order) {
                JuridicalPerson client = juridicalPersonService.selectJuridicalPersonByPrimaryKey(order.getJurClienId());
                if (null != client) {
                    clients.add(client);
                }
            }
        }
        return clients;
    }
}
